package com.centaline.androidsalesblog.wxapi;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.text.TextUtils;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.events.WxStatus;
import com.centaline.androidsalesblog.model.businissobject.ShareBo;
import com.centaline.androidsalesblog.utils.WeiXinUtil;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;

import org.greenrobot.eventbus.EventBus;

import java.util.Locale;

/**
 * 微信登录回调页面
 * Created by guilin on 16/4/11.
 */
public class WXEntryActivity extends BaseActivity {

    private boolean isShare;//是否分享
    private int shareType;//分享类型
    private ShareBo shareBo;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_wx_entry;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar();
    }

    @Override
    protected void initViews() {
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        int type = intent.getIntExtra("_wxapi_command_type", 0);
        switch (type) {
            case 1:
                SendAuth.Resp resp = new SendAuth.Resp(intent.getExtras());
                switch (resp.errCode) {
                    case 0:
                        EventBus.getDefault().post(new WxStatus(0, resp.code));
                        finish();
                        break;
                    default:
                        EventBus.getDefault().post(new WxStatus(1, null));
                        finish();
                        break;
                }
                break;
            case 2:
                SendMessageToWX.Resp messageResp = new SendMessageToWX.Resp(intent.getExtras());
                switch (messageResp.errCode) {
                    case 0:
                        toast("分享成功");
                        finish();
                        break;
                    default:
                        toast("取消分享");
                        finish();
                        break;
                }
                break;
            default:
                break;
        }
    }

    /**
     * 微信分享
     */
    private void share() {
        shareType = getIntent().getIntExtra(ExtraConstant.ShareType, 1);
        shareBo = getIntent().getParcelableExtra(ExtraConstant.ShareContent);
        loadImage();
    }

    private void loadImage() {
        if (TextUtils.isEmpty(shareBo.getImageUrl())) {
            Glide.with(this)
                    .load(R.drawable.ic_centa_square_logo)
                    .override(150, 150)
                    .centerCrop()
                    .into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            Bitmap bitmap = Bitmap.createBitmap(resource.getIntrinsicWidth(),
                                    resource.getMinimumHeight(), Bitmap.Config.RGB_565);
                            Canvas canvas = new Canvas(bitmap);
                            resource.setBounds(0, 0, resource.getIntrinsicWidth(), resource.getIntrinsicHeight());
                            resource.draw(canvas);
                            switch (shareType) {
                                case 1:
                                    weixin(bitmap, false);
                                    break;
                                case 2:
                                    weixin(bitmap, true);
                                    break;
                                default:
                                    break;
                            }
                        }
                    });
        } else {
            Glide.with(this)
                    .load(shareBo.getImageUrl())
                    .override(150, 150)
                    .error(R.drawable.ic_centa_square_logo)
                    .centerCrop()
                    .into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            Bitmap bitmap = Bitmap.createBitmap(resource.getIntrinsicWidth(),
                                    resource.getMinimumHeight(), Bitmap.Config.RGB_565);
                            Canvas canvas = new Canvas(bitmap);
                            resource.setBounds(0, 0, resource.getIntrinsicWidth(), resource.getIntrinsicHeight());
                            resource.draw(canvas);
                            switch (shareType) {
                                case 1:
                                    weixin(bitmap, false);
                                    break;
                                case 2:
                                    weixin(bitmap, true);
                                    break;
                                default:
                                    break;
                            }
                        }
                    });
        }
    }

    /**
     * 微信
     */
    private void weixin(Bitmap bitmap, boolean isScene) {
        WXWebpageObject webpage = new WXWebpageObject();
        WXMediaMessage msg = new WXMediaMessage(webpage);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        switch (shareBo.getComeFrom()) {
            case 0://租售
            case 1:
                webpage.webpageUrl = shareBo.getHouseUrl();

                msg.title = String.format(Locale.CHINA, "%s %s %s %s",
                        shareBo.getHouseName(),
                        shareBo.getHouseType(),
                        shareBo.getArea(),
                        shareBo.getTotalPrice());
                msg.description = shareBo.getHouseDecription();
                msg.thumbData = WeiXinUtil.bmpToByteArray(bitmap, true);
                req.message = msg;
                break;
            case 2://小区
                webpage.webpageUrl = shareBo.getHouseUrl();
                msg.title = String.format(Locale.CHINA, "%s %s %s",
                        shareBo.getHouseName(),
                        shareBo.getRegionGscop(),
                        shareBo.getAvgPrice());
                msg.description = shareBo.getHouseDecription();
                msg.thumbData = WeiXinUtil.bmpToByteArray(bitmap, true);
                req.message = msg;
                break;
            case 3://新房
                webpage.webpageUrl = shareBo.getHouseUrl();
                msg.title = isScene ? String.format(Locale.CHINA, "%s %s %s",
                        shareBo.getHouseName(),
                        shareBo.getRegionGscop(),
                        shareBo.getAvgPrice()) :
                        shareBo.getHouseName();
                msg.description = String.format(Locale.CHINA, "%s %s",
                        shareBo.getRegionGscop(),
                        shareBo.getAvgPrice());
                msg.thumbData = WeiXinUtil.bmpToByteArray(bitmap, true);
                req.message = msg;
                break;
            case 4://经纪人店铺
                webpage.webpageUrl = shareBo.getHouseUrl();
                msg.title = isScene ? shareBo.getHouseDecription() : shareBo.getHouseName();
                msg.description = shareBo.getHouseDecription();
                msg.thumbData = WeiXinUtil.bmpToByteArray(bitmap, true);
                req.message = msg;
                break;
            default:
                break;
        }
        req.scene = isScene ? SendMessageToWX.Req.WXSceneTimeline
                : SendMessageToWX.Req.WXSceneSession;
        req.transaction = String.format(Locale.CHINA, "webpage%d", System.currentTimeMillis());
//        iwxapi.sendReq(req);
    }
}
